package com.tal.pad.eye_protection.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.VideoView;

import java.io.IOException;

public class VideoUtils {
    private static final int MSG_CAPTURE_FRAME = 1;
    private static final long FRAME_CAPTURE_INTERVAL = 100; // 每 100 毫秒捕获一帧

    private VideoView videoView;
    private MediaMetadataRetriever retriever;
    private Handler handler;
    private OnFrameCapturedListener listener;
    private boolean isVideoViewPrepared=false;
    private static VideoUtils instance;

    public static VideoUtils getInstance(){
        if (instance==null){
            instance=new VideoUtils();
        }
        return instance;
    }

    public void init(Context context, VideoView videoView, Uri videoUri, OnFrameCapturedListener listener) {
        this.videoView = videoView;
        this.listener = listener;
        if (retriever==null){
            retriever = new MediaMetadataRetriever();
        }
        retriever.setDataSource(context, videoUri);
        isVideoViewPrepared=false;
        videoView.setVideoURI(videoUri);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isVideoViewPrepared=true;
//                AwConDetectApi.getInstance().resetResult();
                starVido();
            }
        });
        if (handler==null){
            handler = new Handler(Looper.getMainLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == MSG_CAPTURE_FRAME) {
                        if (videoView.isPlaying()) {
                            int currentPosition = videoView.getCurrentPosition();
                            Bitmap frame = retriever.getFrameAtTime(currentPosition * 1000);
                            if (frame != null && listener != null) {
                                listener.onFrameCaptured(frame);
                            }
                        }
                        // 继续发送下一次捕获消息
                        sendEmptyMessageDelayed(MSG_CAPTURE_FRAME, FRAME_CAPTURE_INTERVAL);
                    }
                }
            };
        }

    }

    private void startCapturingFrames() {
        handler.sendEmptyMessage(MSG_CAPTURE_FRAME);
    }
    public void starVido(){
        if (isVideoViewPrepared){
            startCapturingFrames();
            videoView.start();
        }else {
            Log.e("++ Mr.Zan ++", "starVido: videoView未准备好" );
        }
    }
    public void stopCapturingFrames() throws IOException {
        if (handler != null) {
            handler.removeMessages(MSG_CAPTURE_FRAME);
        }
        if (retriever != null) {
            retriever.release();
            retriever = null;
        }
    }

    public interface OnFrameCapturedListener {
        void onFrameCaptured(Bitmap frame);
    }


}
